package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author by mycp
 * @Classname UserController
 * @Description TODO
 * @Date 2018/11/1 12:56
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IGreatService greatService;

    @Autowired
    private ISystemNoticeService systemNoticeService;

    @Autowired
    private IFocusService focusService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private ISignService signService;

    @Autowired
    private IStrategyTagsService strategyTagsService;

    /**
     * 资源: /users
     * 动作: POST
     * 参数: email = xxx & nickName=xxx & password=xxx
     *
     * @param user
     * @return
     */
    @PostMapping
    @ResponseBody
    public Object register(User user) {
        // 设置一个jsonResult数据用于响应客户端的数据
        JsonResult result = new JsonResult();

        // 用户注册需要先检查用户名是否已被注册, 调用service方法
        try {
            userService.register(user);
            result.setData(user);
        } catch (Exception e) {
            // 打印异常信息
            e.printStackTrace();
            // 设置异常信息到响应数据中
            result.mark(e.getMessage());

        }
        return result;
    }

    /**
     * 根据游记作者id查询作者信息
     *
     * @param id
     * @return
     */
    @GetMapping
    @ResponseBody
    public Object register(Long id) {
        JsonResult result = new JsonResult();
        try {
            User user = userService.get(id);
            result.setData(user);
        } catch (Exception e) {
            // 打印异常信息
            e.printStackTrace();
            // 设置异常信息到响应数据中
            result.mark(e.getMessage());

        }
        return result;
    }

    /**
     * 更新用户个人资料
     * 资源:/users/{id}
     * 动作: put
     * 参数: nickName=xxx&place=xxx...
     */
    @PutMapping("{id}")
    @ResponseBody
    public Object updateUserInfo(User user) {
        JsonResult result = new JsonResult();
        try {
            userService.saveOrUpdate(user);
            result.setData(user);
        } catch (Exception e) {
            // 打印异常信息
            e.printStackTrace();
            // 设置异常信息到响应数据中
            result.mark(e.getMessage());

        }
        return result;
    }

    /**
     * 查询游记信息
     * 接口: /users/{authorId}/travels
     * 参数: currentPage=x
     * 动作: GET
     */
    @GetMapping("/{authorId}/travels")
    @ResponseBody
    public Object queryTravels(TravelQueryObject qo) {
        // 设置qo中的根据最后跟新时间进行排序查询
        qo.setOrderBy("t.lastUpdateTime desc");
        PageInfo pageInfo = travelService.queryTravel(qo);
        return pageInfo;
    }

    /**
     * 保存游记信息
     * 接口: /users/{authorId}/travels
     * 参数: 所有游记相关的数据
     * 动作: POST
     */
    @PostMapping("/{authorId}/travels")
    @ResponseBody
    public Object saveTravels(Travel entity) {
        JsonResult result = new JsonResult();
        // 设置用户/作者信息
        entity.setAuthor(UserContext.getCurrentUser());
        try {
            travelService.saveOrUpdate(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("服务器繁忙, 请稍后再试");
        }
       return result;
    }

    /**
     * 更新用户的指定游记
     * @param entity
     * @return
     */
    @PutMapping("/{authorId}/travels")
    @ResponseBody
    public Object updateTravels(Travel entity) {
        JsonResult result = new JsonResult();
        travelService.saveOrUpdate(entity);
        return result;
    }

    /**
     * 接口: /users/{sponsorId}/sponsor/number
     * 动作: GET
     * 参数:
     * 获取指定用户未阅读的点赞数
     */
    @GetMapping("/{id}/sponsors/number")
    public Object selectSponsorNumber(@PathVariable Long id) {
        Integer number = greatService.selectSponsorNumber(id);
        if (number < 1) {
            number = null;
        }
        return number;
    }

    /**
     * 接口: /users/{sponsorId}/sponsor
     * 动作: GET
     * 参数:
     * 获取指定用户未阅读的点赞信息内容
     */
    @GetMapping("/{authorId}/sponsors/content")
    public Object selectBySponsorId(TravelQueryObject qo) {
        qo.setOrderBy("g.greatTime desc");
        PageInfo greats = greatService.selectByAuthorId(qo);
        return greats;
    }

    /**
     * 接口: /users/{userId}/notice/number
     * 动作: GET
     * 参数:
     * 获取指定用户未阅读的系统消息数
     */
    @GetMapping("/{id}/notice/number")
    public Object selectNoticeNumber(@PathVariable Long id) {
        Integer number = systemNoticeService.selectNoticeNumber(id);
        if (number < 1) {
            number = null;
        }
        return number;
    }

    /**
     * 接口: /users/{userId}/notice
     * 动作: GET
     * 参数:
     * 获取指定用户未阅读的系统消息内容
     */
    @GetMapping("/{userId}/notice")
    public Object selectSystemNotice(@PathVariable Long userId) {
        List<SystemNotice> systemNotices = systemNoticeService.selectSystemNotice(userId);
        return systemNotices;
    }

    /**
     * 增加关注的人的接口
     *
     * @param focusId   当前操作的人id
     * @param focusedId 关注人的id
     * @return
     */
    @PostMapping("/{focusId}/focuseds")
    @ResponseBody
    public Object saveFocus(@PathVariable Long focusId, Long focusedId) {
        JsonResult result = new JsonResult();
        focusService.saveFocus(focusId, focusedId);
        return result;
    }

    /**
     * 取消关注的接口
     *
     * @param focusId
     * @param focusedId
     * @return
     */
    @DeleteMapping("/{focusId}/focuseds/{focusedId}")
    @ResponseBody
    public Object deleteFocus(@PathVariable Long focusId, @PathVariable Long focusedId) {
        JsonResult result = new JsonResult();
        focusService.deleteFocus(focusId, focusedId);
        return result;
    }

    /**
     * 用于判断当前操作人有无关注当前游记作者的接口
     *
     * @param focusId   当前操作人id
     * @param focusedId 当前游记作者id
     * @return
     */
    @GetMapping("/{focusId}/focuseds")
    @ResponseBody
    public Object getFocus(@PathVariable Long focusId, Long focusedId) {
        JsonResult result = new JsonResult();
        try {
            focusService.get(focusId, focusedId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 查询当前id的关注数和粉丝数的接口
     *
     * @param focusId
     * @return
     */
    @GetMapping("/{focusId}/counts")
    @ResponseBody
    public Object counts(@PathVariable Long focusId) {
        JsonResult result = new JsonResult();
        try {
            Map map = focusService.getCount(focusId);
            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    /**
     * 保存攻略点评
     * 接口: /users/{authorId}/strategyComments
     * 参数: 所有游记相关的数据
     * 动作: POST
     */
    @PostMapping("/{authorId}/strategyComments")
    @ResponseBody
    public Object saveStrategyComments(StrategyComment entity, String[] images,String[] tags,String[] myTags) {
        JsonResult result = new JsonResult();
        // 设置点评人信息
        entity.setUser(UserContext.getCurrentUser());
        try {
            strategyCommentService.saveOrUpdate(entity, images);
            //保存标签
            strategyTagsService.saveOrUpdate(entity.getStrategy().getId(),tags,myTags);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("服务器繁忙, 请稍后再试");

        }
        return result;
    }


    /**
     * 根据作者id查询他名校所有的攻略点评
     * 接口: /users/{authorId}/strategyComments
     * 参数: 所有游记相关的数据
     * 动作: POST
     */
    @GetMapping("/{authorId}/strategyComments")
    public Object getStrategyComments(StrategyCommentQueryObject qo) {
        // 根据作者id查询所有名下所有的攻略点评
        qo.setOrderBy("scm.releaseTime desc");
        PageInfo pageInfo = strategyCommentService.getStrategyCommentsByAuthorId(qo);

        return pageInfo;
    }

    /**
     * 查询用户的积分
     * 接口: /users/{userId}/points
     * 参数: userId = xx
     * 动作: GET
     */
    @GetMapping("/{userId}/points")
    public Object getPointsByUserId(@PathVariable Long userId){
        Sign signByUserId = signService.getSignByUserId(userId);
        return signByUserId.getSignPoints();
    }

    /**
     * 验证用户的密码
     * 接口:/users
     * 动作: GET
     * 参数: userId=xx&password=xx
     */
    @PostMapping("/orders")
    public Object checkPassword(Long userId, String password, String email){
        JsonResult result = new JsonResult();
        User user = null;
        try {
            user = userService.login(password, email);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
